package com.kunsan.compose

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Tab
import androidx.compose.material3.TabPosition
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController


@Composable
fun HomeScreen(navController: NavHostController) {

    Scaffold(
        modifier = Modifier.background(Color.Gray),
        topBar = { TopBar() }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .padding(
                    top = innerPadding.calculateTopPadding() + 10.dp,
                    start = 5.dp,
                    end = 5.dp
                )
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Row {
                Column(
                    modifier = Modifier
                        .fillMaxWidth(0.5f)
                ) {
                    Box(
                        modifier = Modifier
                            .clip(RoundedCornerShape(12.dp))
                            .fillMaxWidth()
                            .background(Color.Red)
                            .height(100.dp)
                            .offset(x = (-15).dp),
                        contentAlignment = Alignment.Center

                    ) {
                        Text("预约会议", style = TextStyle(fontSize = 18.sp, color = Color.White))
                    }
                    Spacer(modifier = Modifier.height(5.dp))
                    Box(
                        modifier = Modifier
                            .clip(RoundedCornerShape(12.dp))
                            .fillMaxWidth()
                            .background(Color.Blue)
                            .height(100.dp)
                            .offset(x = (-15).dp)
                            .clickable {
                                navController.navigate(MeetingDestinations.ROOM_ROUTE)
                            },
                        contentAlignment = Alignment.Center

                    ) {
                        Text("加入会议", style = TextStyle(fontSize = 18.sp, color = Color.White))
                    }
                }

                Spacer(modifier = Modifier.width(5.dp))

                Box(
                    modifier = Modifier
                        .clip(RoundedCornerShape(12.dp))
                        .fillMaxWidth()
                        .background(Color.Green)
                        .height(205.dp),
                    contentAlignment = Alignment.Center
                ) {
                    Text("快速会议", style = TextStyle(fontSize = 18.sp, color = Color.White))
                }
            }

            Spacer(modifier = Modifier.height(15.dp))

            val list = listOf("当前", "历史")

            TabRow(selectedTabIndex = 0,
                containerColor = Color(0xff1E76DA),
                modifier = Modifier
                    .padding(vertical = 4.dp, horizontal = 8.dp)
                    .clip(RoundedCornerShape(50))
                    .padding(1.dp)
                    .fillMaxWidth(0.4f)
                    .height(30.dp),
                indicator = { tabPositions: List<TabPosition> ->
                    Box {}
                }
            ) {
                list.forEachIndexed { index, text ->
                    val selected = 0 == index
                    Tab(
                        modifier = if (selected) Modifier
                            .clip(RoundedCornerShape(50))
                            .background(
                                Color.White
                            ).height(20.dp)
                        else Modifier
                            .clip(RoundedCornerShape(50))
                            .background(
                                Color(
                                    0xff1E76DA
                                )
                            ).height(26.dp),
                        selected = selected,
                        onClick = { },
                        text = { Text(text = text, color = Color(0xff6FAAEE)) }
                    )
                }
            }
        }
    }
}

@Composable
fun TopBar() {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(54.dp)
            .background(Color.White)
            .padding(horizontal = 16.dp, vertical = 8.dp), // 先后有讲究
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            modifier = Modifier
                .size(32.dp)
                .clip(CircleShape),
            painter = painterResource(id = R.drawable.ic_launcher_background),
            contentDescription = null
        )
        Spacer(modifier = Modifier.width(8.dp))
        Column {
            Text("昵称", style = TextStyle(fontSize = 14.sp))
            Text("公司", style = TextStyle(fontSize = 10.sp))
        }
    }
}


@Preview(showBackground = true)
@Composable
fun HomeScreenPreview() {
    HomeScreen(rememberNavController())
}